<?php
/**
 * @Author: lianlianzan(13040@qq.com)
 * @Date:   2018-08-18 16:40:16
 * @Last Modified by:   lianlianzan
 * @Last Modified time: 2020-11-15 11:14:38
 * 授权管理控制器
 */

namespace app\admin\logic;

use eacoo\Tree;
use think\Cookie;

class AdminAuth extends AdminLogic {

	protected function initialize() {
		parent::initialize();
		
		$this->authRuleModel  = model('admin/AuthRule');
		$this->authGroupModel = model('admin/AuthGroup');
		$this->userModel      = model('admin/AdminUser');

	}
	
	/**
	 * 获取侧边栏菜单
	 * @param    string                    $position [description]
	 * @return   [type]                              [description]
	 * @Author   lianlianzan(13040@qq.com)
	 * @DateTime 2020-11-07
	 */
	public function getAdminSidebarMenu($groupid) {
		$module = strtolower(MODULE_NAME);
		//echo $module;exit;

// 		if (0 && $module == 'weixin_manage') {
// 			$isIndependence = 1;
// 			$sideBarMenuCate = include_once(APP_PATH.'weixin_manage/menu.php');
// 			//echo '菜单数组:';print_r($sideBarMenuCate);exit;
// 		} else {
// 			//echo '菜单树:';print_r($sidebarMenuGroup);exit;
// 		}
		
		//获取菜单分类
		$authCateDao = model('admin/AuthCate');
		$sideBarMenuCate = $authCateDao->getMenuCate($groupid);
		//echo '菜单分类:';print_r($sideBarMenuCate);
		
		//根据组别获取菜单的树
		$authRuleDao = model('admin/AuthRule');
		list($menuTree, $selfMenuRule, $subsidiaryArr) = $authRuleDao->getNewMenuTree($groupid);
		//echo '菜单树:';print_r($menuTree);
		//echo '当前菜单';print_r($selfMenuRule);exit;
		if ($selfMenuRule['is_selfmenu'] == 1) {
			$isSelfmenu = 1;
			$sideBarMenuCate = include(APP_PATH.$module.'/config/menu.php');
			//print_r($sideBarMenuCate);exit;
		} else {
			$isSelfmenu = 0;
		}
		if ($sideBarMenuCate) {
			$sidebarMenuGroup = array();
			foreach ($sideBarMenuCate as $key => $value) {
				$tmpMenu = array();
				//把树拼凑给每个分类
				foreach ($menuTree as $key2 => $value2) {
					if ($value2['cid'] == $value['id']) {
						$tmpMenu[] = $value2;
					}
				}
				$sidebarMenuGroup[$value['id']] = $tmpMenu;
			}
		}
		return array($isSelfmenu, $sideBarMenuCate, $sidebarMenuGroup, $subsidiaryArr);
	}

	/**
	 * 检测规则权限
	 */
	public function checkAuth($groupid, $isSelfmenu, $subsidiaryArr) {
		$module = MODULE_NAME; //模块名(项目名)
		$controller = CONTROLLER_NAME;
		if ($module == 'admin' && $controller == 'Index') {
			return array(1);
		}
		$module = strtolower($module);
		$controller = strtolower($controller);
		//echo 'module:'.$module;echo '<br>controller:'.$controller;exit;
		//print_r($subsidiaryArr);exit;

		if ($isSelfmenu == 1) { //独立菜单的情况
			$data = array(
				'groupid' 	=> $groupid,
				'm'			=> $module,
			);
			$authAccessDao = model('admin/AuthAccess');
			$count = $authAccessDao->where($data)->count();
			//echo 'getLastSql:';echo $authAccessDao->getLastSql();
			return array($count);
		} else {
			$data = array(
				'groupid' 	=> $groupid,
				'm'			=> $module,
				'c'			=> $controller,
			);
			//print_r($data);
			$authAccessDao = model('admin/AuthAccess');
			$count = $authAccessDao->where($data)->count();
			//echo 'getLastSql:';echo $authAccessDao->getLastSql();
			if ($count > 0) {
				return array($count);
			} else {
				foreach ($subsidiaryArr as $key => $value) {
					if (strpos(strtolower($value['subsidiary']), $controller) !== false) {
						//echo '父级菜单:';print_r($value);exit;
						$data = array(
							'groupid' 	=> $groupid,
							'm'			=> $module,
							'c'			=> $value['controller'],
						);
						$authAccessDao = model('admin/AuthAccess');
						$count = $authAccessDao->where($data)->count();
						return array($count, $value);
					}
				}
			}
		}
	 }
}